package org.zeapp.opencloud.framework.cloud.impl.dropbox;

import java.awt.Container;

import org.zeapp.opencloud.framework.cloud.Cloud;
import org.zeapp.opencloud.framework.cloud.CloudDriver;
import org.zeapp.opencloud.framework.cloud.CloudDriverManager;
import org.zeapp.opencloud.framework.cloud.CloudException;
import org.zeapp.opencloud.framework.preference.CallbackHandler;
import org.zeapp.opencloud.framework.preference.Preference;

public class DropBoxDriver implements CloudDriver {
	public static final String TOKEN_KEY = "authentication_token";

	private static final DropBoxDriver INSTANCE = new DropBoxDriver();
	private static volatile boolean registered = false;

	static {
		load();
	}

	private PreferencePanel panel;

	/**
	 * Register the driver on CloudDriverManager, if not already done, and
	 * return the driver singleton.
	 * 
	 * @return the driver singleton
	 */
	public static synchronized DropBoxDriver load() {
		if (!registered) {
			CloudDriverManager.registerDriver(INSTANCE);
			registered = true;
		}
		return INSTANCE;
	}

	@Override
	public String getName() {
		return "Dropbox";
	}

	/**
	 * Return true if the identifier is equals to "Dropbox".
	 */
	@Override
	public boolean accepts(String identifier) {
		return "Dropbox".equals(identifier);
	}

	@Override
	public void buildPreferenceScreen(Object UIContainer,
			Preference preference, CallbackHandler exceptionHandler) {
		panel = new PreferencePanel();
		panel.initContent(preference, exceptionHandler);
		((Container) UIContainer).add(panel);

	}

	@Override
	public boolean applyPreferenceScreenUpdate(Preference preference) {
		return panel.apply(preference);
	}

	@Override
	public Cloud getCloud(Preference preference) throws CloudException {
		return new DropboxCloud(preference);
	}
}
